import { defineStore } from 'pinia'

export const useThemeStore = defineStore('theme', {
  state: () => ({
    isDark: false,
    primaryColor: '#00CC99',
    secondaryColor: '#FFCC00',
    backgroundColor: '#F0F8FF',
    textColor: '#666666'
  }),
  
  actions: {
    toggleTheme() {
      this.isDark = !this.isDark
      if (this.isDark) {
        document.documentElement.style.setProperty('--background-color', '#1a1a1a')
        document.documentElement.style.setProperty('--text-color', '#ffffff')
        document.documentElement.style.setProperty('--border-color', '#333333')
        document.documentElement.style.setProperty('--table-row-hover', '#2a2a2a')
      } else {
        document.documentElement.style.setProperty('--background-color', this.backgroundColor)
        document.documentElement.style.setProperty('--text-color', this.textColor)
        document.documentElement.style.setProperty('--border-color', '#E0E0E0')
        document.documentElement.style.setProperty('--table-row-hover', '#f7fcff')
      }
    },
    
    updatePrimaryColor(color) {
      this.primaryColor = color
      document.documentElement.style.setProperty('--primary-color', color)
      document.documentElement.style.setProperty('--primary-hover-color', this.adjustColor(color, -10))
    },
    
    updateSecondaryColor(color) {
      this.secondaryColor = color
      document.documentElement.style.setProperty('--secondary-color', color)
    },
    
    // 辅助函数：调整颜色明度
    adjustColor(color, percent) {
      const num = parseInt(color.replace('#', ''), 16)
      const amt = Math.round(2.55 * percent)
      const R = (num >> 16) + amt
      const G = (num >> 8 & 0x00FF) + amt
      const B = (num & 0x0000FF) + amt
      return '#' + (0x1000000 +
        (R < 255 ? (R < 1 ? 0 : R) : 255) * 0x10000 +
        (G < 255 ? (G < 1 ? 0 : G) : 255) * 0x100 +
        (B < 255 ? (B < 1 ? 0 : B) : 255)
      ).toString(16).slice(1)
    }
  }
}) 